<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          所属类型:
          <el-select v-model="queryInfo.type" placeholder="请选择" style="width: 73%" size="mini" @change="loadData()">
            <el-option v-for="item in menulist" :key="item.id" :label="item.authName" :value="item.id"> </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          编号:
          <el-input style="width: 100px" size="mini" v-model="queryInfo.number"></el-input>
        </el-col>
        <el-col :span="4">
          牌号:
          <el-input style="width: 100px" size="mini" v-model="queryInfo.mark"></el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="mini" @click="queryData">查询</el-button>
        </el-col>
      </el-row>
      <el-row style="margin: 10px 0 10px">
        <el-col :span="2">
          <el-button type="primary" size="mini" @click="addParameterVisible = true"><i class="el-icon-plus"></i>添加</el-button>
        </el-col>
        <el-col :span="2" style="margin-left: 10px">
          <el-button type="primary" size="mini" @click="showImport"><i class="el-icon-arrow-right"></i>导入</el-button>
        </el-col>
      </el-row>
      <!-- 两种放在一起 -->
      <el-table :data="parameterList" border height="300px" style="margin: 10px 0 10px">
        <el-table-column align="center" type="index"></el-table-column>
        <el-table-column v-if="tableHeader[0]" prop="grade" align="center" label="牌号"></el-table-column>
        <el-table-column v-if="tableHeader[1]" prop="diameter" align="center" label="直径(m)"></el-table-column>
        <el-table-column v-if="tableHeader[2]" prop="mass" align="center" label="质量(kg)"></el-table-column>
        <el-table-column v-if="tableHeader[3]" prop="height" align="center" label="高度(m)"></el-table-column>
        <el-table-column v-if="tableHeader[4]" prop="extent" align="center" label="长度(m)"></el-table-column>
        <el-table-column v-if="tableHeader[5]" prop="length" align="center" label="截面长度(m)"></el-table-column>
        <el-table-column v-if="tableHeader[6]" prop="width" align="center" label="截面宽度(m)"></el-table-column>
        <el-table-column v-if="tableHeader[7]" prop="area" align="center" label="截面积(m^2)"></el-table-column>
        <el-table-column v-if="tableHeader[8]" prop="massDensity" align="center" label="实际质量密度(kg/m^3)" width="170"></el-table-column>
        <el-table-column v-if="tableHeader[9]" prop="volume" align="center" label="体积(m^3)"></el-table-column>
        <el-table-column v-if="tableHeader[10]" prop="remarks" align="center" label="备注"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="showEdit(scope.row)"> </el-button>
            <el-button type="text" icon="el-icon-delete" @click="showDel(scope.row.id)"> </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[10, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
    <!-- 添加样本规格信息 -->
    <el-dialog title="编辑样品规格信息" :visible.sync="addParameterVisible" width="70%">
      <el-form :inline="true">
        <el-form-item label="样品编号:" label-width="170px">
          <el-select :disabled="isedit" v-model="parameter.productId" placeholder="请选择" style="width: 191px">
            <el-option v-for="item in productIdList" :key="item.id" :label="item.id" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属类型:" label-width="170px">
          <el-select :disabled="isedit" v-model="parameter.specification" placeholder="请选择" style="width: 191px">
            <el-option v-for="item in menulist" :key="item.id" :label="item.authName" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牌号:" label-width="170px">
          <el-input v-model="parameter.garde"></el-input>
        </el-form-item>
        <el-form-item label="直径:" label-width="170px">
          <el-input v-model="parameter.diameter"></el-input>
        </el-form-item>
        <el-form-item label="质量:" label-width="170px">
          <el-input v-model="parameter.mass"></el-input>
        </el-form-item>
        <el-form-item label="高度:" label-width="170px">
          <el-input v-model="parameter.height"></el-input>
        </el-form-item>
        <el-form-item label="截面长度:" label-width="170px">
          <el-input v-model="parameter.length"></el-input>
        </el-form-item>
        <el-form-item label="截面宽度:" label-width="170px">
          <el-input v-model="parameter.width"></el-input>
        </el-form-item>
        <el-form-item label="截面积:" label-width="170px">
          <el-input v-model="parameter.area"></el-input>
        </el-form-item>
        <el-form-item label="实际质量密度:" label-width="170px">
          <el-input v-model="parameter.massDensity"></el-input>
        </el-form-item>
        <el-form-item label="体积:" label-width="170px">
          <el-input v-model="parameter.volume"></el-input>
        </el-form-item>
        <el-form-item label="备注:" label-width="170px">
          <el-input v-model="parameter.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParameterVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParameter">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除提示对话框 -->
    <el-dialog title="提示" :visible.sync="delDiaVisible" width="50%">
      <span style="font-size: 20px; display: inline-block; align: center">确定删除此数据吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDiaVisible = false">取 消</el-button>
        <el-button type="primary" @click="del()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 导入按钮 -->
    <el-dialog title="提示" :visible.sync="importTag" width="60%">
      <div style="font-size: 20px; margin-left: 40px">请下载导入模板进行导入</div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" style="margin-right: 20px" @click="exportData">下载模板</el-button>
        <a-upload name="file" :action="uploadUrl" accept=".xls,.xlsx" :data="paramData" :multiple="true" @change="handleChange" :showUploadList="false">
          <el-button type="primary">已有模板，立即导入</el-button>
        </a-upload>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      importTag: false,
      paramData: {
        name: '2'
      },
      uploadUrl: 'http://59.172.104.174:30669/admin/uploadLessonFile',
      isedit: false,
      parameter: {
        id: '',
        productId: '',
        diameter: '',
        mass: '',
        height: '',
        length: '',
        width: '',
        area: '',
        massDensity: '',
        volume: '',
        remarks: ''
      },
      //[牌号，直径，质量，高度，长度，截面长度，截面宽度，截面积，实际质量密度，体积，备注]
      tableHeader: [],
      circleControl: [1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1],
      squareControl: [1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1],
      delDiaVisible: false,
      delId: '',
      //类型选择
      menulist: [
        {
          id: '圆柱',
          authName: '圆柱'
        },
        {
          id: '方柱',
          authName: '方柱'
        }
      ],
      parameterList: [],
      productIdList: [],
      addParameterVisible: false,
      total: 0,
      queryInfo: {
        // number: '',
        // mark: '',
        // supplier: '',
        pageNum: 1,
        pageSize: 10,
        type: '圆柱'
      }
    }
  },
  created() {
    this.loadData()
    this.getAllProductId()
  },
  methods: {
    handleChange(info) {
      if (info.file.status === 'done') {
        if (info.file.response.code == '201') {
          this.$message.success('重新上传文件成功！')
        } else if (info.file.response.code == '200') {
          this.$message.success('上传文件成功！')
        }
      }
    },
    //模板导出按钮
    exportData() {
      this.axios({
        baseURL: 'http://59.172.104.174:30669',
        url: `/admin/downLoadLessonFile?name=牌号标签`,
        methods: 'GET',
        responseType: 'blob'
      }).then(res => {
        const blob = new Blob([res.data])
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.download = `牌号标签.xlsx`
        link.href = url
        link.click()
      })
    },
    showImport() {
      this.importTag = true
    },
    showEdit(row) {
      this.addParameterVisible = true
      this.parameter = row
    },
    //添加样品规格信息
    addParameter() {
      this.axios({
        baseURL: 'http://59.172.104.174:30669',
        url: '/perMagneticSpecification/insertorupdateSimpleSize',
        method: 'POST',
        data: this.parameter
      }).then(res => {
        if (res.data.code == 200) {
          this.$message.success('添加样品规格信息成功！！！')
          this.addParameterVisible = false
          this.loadData()
        }
      })
    },
    showDel(id) {
      //删除弹框
      this.delDiaVisible = true
      this.delId = id
    },
    del() {
      //发起删除请求
      this.axios({
        baseURL: '',
        url: '/perMagneticSpecification/deleteSimpleSize',
        methods: 'GET'
      }).then(res => {
        if (res.data.code == 200) {
          this.$message.success('删除成功')
        }
      })
    },
    queryData() {},
    getAllProductId() {
      this.axios({
        baseURL: 'http://59.172.104.174:30669',
        url: '/perMagnetParameter/selectParameter',
        method: 'POST',
        data: {
          pageNum: 1,
          pageSize: 100
        }
      }).then(res => {
        this.productIdList = res.data.result.records.map(x => {
          return new Object({ id: x.id })
        })
      })
    },
    loadData() {
      this.parameter.specification = this.queryInfo.type
      this.queryInfo.type == '圆柱' ? (this.tableHeader = this.circleControl) : (this.tableHeader = this.squareControl)
      this.axios({
        baseURL: 'http://59.172.104.174:30669',
        url: `/perMagneticSpecification/selectSimpleSize`,
        method: 'POST',
        data: this.queryInfo
      }).then(res => {
        this.parameterList = res.data.result.list.records
      })
    },
    showImport() {},
    //分页控制
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
    }
  }
}
</script>

<style scoped></style>
